package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;

    //同时操作两张表，开启事务
    @Transactional
    public void saves(DishDto dishDto) {
        //菜品表-》获得ID-》把id塞入菜品口味表-》批量保存
        //保存菜品的基本信息到菜品表
        this.save (dishDto);
        Long dishid=dishDto.getId ();
        //菜品口味，把id塞进去
        List<DishFlavor> flavors=dishDto.getFlavors ();
        flavors=flavors.stream ().map ((item) -> {
            item.setDishId (dishid);
            return item;
        }).collect (Collectors.toList ());
        //保存菜品口味数据在flavors中，批量保存
        dishFlavorService.saveBatch (flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        //菜品表获得id-》拷贝到dto-》比对id(菜品、口味)-》列出集合，存到dto的flavors集合
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    @Transactional
    //更新菜品表-》更改口味表（清理（对比id清理）/添加）
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);
        //把id塞进去
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
     //没有封装dishid
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }
    @Override
    @Transactional
    public void remove(List<Long> ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Dish> qw1=new LambdaQueryWrapper<> ();
        qw1.in (Dish::getId,ids);
        //销售状态为1的不能卖
        qw1.eq(Dish::getStatus,1);
        int count=this.count (qw1);
        if(count>0){
            throw new CustomException ("餐品在销售，删除失败");
        }
        //如果可以删除，先删除口味表中的数据
        this.removeByIds (ids);
        LambdaQueryWrapper<DishFlavor> qw2=new LambdaQueryWrapper<> ();
        qw2.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove (qw2);
    }
//分页查询
    @Override
    public R<IPage> findByPage(int page, int pageSize, String name) {
        Page<DishDto> pages=new Page<> (page, pageSize);
        List<DishDto> byPage=dishMapper.findByPage (pages, name);
        pages.setRecords (byPage);
        return R.success (pages);
    }

}
